package com.redis.sample.config;

import com.redis.sample.common.redis.RedisProperties;
import com.redis.sample.common.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.util.StringUtils;

@Slf4j
public class RedissonConfig {

  public static RedissonClient getRedissonClient (){
    RedisProperties redisProperties = new RedisProperties();
    redisProperties.setMode("single");
    redisProperties.setUrl("redis://192.168.37.101:6379");
    redisProperties.setPwd("");

    Config config = new Config();
    config.setCodec(new JsonJacksonCodec(JsonUtils.mapper));

    SingleServerConfig singleServerConfig = config.useSingleServer();
    log.info("redis host: {}", redisProperties.getUrl());
    singleServerConfig.setAddress(redisProperties.getUrl()).setConnectionPoolSize(32);
    if (StringUtils.hasLength(redisProperties.getPwd())) {
      singleServerConfig.setPassword(redisProperties.getPwd());
    }
    return Redisson.create(config);
  }

}
